package com.qiniuyun.backai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiniuyun.backai.mapper.ChatMapper;
import com.qiniuyun.backai.model.pojo.Chat;
import com.qiniuyun.backai.service.ChatService;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author wlf
 * @description 针对表【chat(用户)】的数据库操作Service实现
 */
@Service
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements ChatService {



    @Override
    public Set<String> getChatIds(String type) {
        QueryWrapper<Chat> chatQueryWrapper = new QueryWrapper<>();
        chatQueryWrapper.select("id").eq("type", type);
        Set<String> ids = this.list(chatQueryWrapper)
                .stream()
                .map(chat -> String.valueOf(chat.getId()))
                .collect(Collectors.toSet());
        if (ids == null) {
            return new HashSet<String>();
        }
        return ids;
    }

    /**
     * 更新内容通过会话id
     * @param chat
     * @param convId
     * @return
     */
    @Override
    public Integer updateByConvId(Chat chat, String convId) {
        UpdateWrapper<Chat> wrapper = new UpdateWrapper<Chat>().eq("convId", convId);
        int update = this.baseMapper.update(chat, wrapper);
        return update;
    }

    @Override
    public Chat getByConvId(String convId) {
        QueryWrapper<Chat> convId1 = new QueryWrapper<Chat>().eq("convId", convId);
        Chat chat = this.baseMapper.selectOne(convId1);
        return chat;
    }

    @Override
    public Integer deleteByConvId(String convId) {
        QueryWrapper<Chat> wrapper = new QueryWrapper<Chat>().eq("convId", convId);
        int delete = this.baseMapper.delete(wrapper);
        return delete;
    }
}


